package com.flyme.module.exam.provider.service.impl;




import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.common.core.utils.ObjectUtils;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.core.base.supper.service.impl.BaseServiceImpl;
import com.flyme.module.exam.client.entity.BusUserability;
import com.flyme.module.exam.client.entity.ExaDuan;
import com.flyme.module.exam.client.entity.ExamType;
import com.flyme.module.exam.client.enums.AbilityItemEnum;
import com.flyme.module.exam.provider.mapper.BusUserabilityMapper;
import com.flyme.module.exam.provider.service.BusUserabilityService;
import com.flyme.module.exam.provider.service.ExaDuanService;
import com.flyme.module.exam.provider.service.ExamTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author: flyme
 * @date: 2018/3/7 15:27
 * @desc: 部门接口实现
 */
@Service
public class BusUserabilityServiceImpl extends BaseServiceImpl<BusUserabilityMapper, BusUserability> implements BusUserabilityService {

    @Autowired
    private ExaDuanService duanService;
    @Autowired
    private ExamTypeService examTypeService;

    /**
     * 计算用户hr得分
     */
    public Map<String, Object> countUserHr(Long accountId){
        return getMap(q().select("SUM(hrScore) score").eq(true,"userId", accountId));
    }

    /**
     * 计算用户项目得分
     */
    public String countUser(Long accountId, String abilityItemId){
        BusUserability userability = getOne(q().eq(true,"userId", accountId).eq(true,"abilityItemId", abilityItemId) ,false);
        if(ObjectUtils.isNotEmpty(userability)){
            return userability.getAbilityScore();
        }
        return null;
    }

    /**
     * 统计用户测评条目
     */
    public Integer countByUser(Long userId){
        return count(q().eq("userId", userId));
    }

    /**
     * 计算除工作教育综合以外的其他项目得分
     */
    public Map<String, Object> countOther(Long accountId, String itemWork, String itemStudy, String itemSynthesis){
        return getMap(q().select("SUM(abilityScore) score").ne("abilityItemId", itemWork).ne("abilityItemId", itemStudy).ne("abilityItemId", itemSynthesis).eq("userId", accountId));
    }

    /**
     * 查询用户的项目得分及段位
     */
    public BusUserability findUser(Long accountId, Long abilityItemId){
        BusUserability userability = getOne(q().eq(true, "userId", accountId).eq(true, "abilityItemId", abilityItemId), false);
        return userability;
    }

    /**
     * 初始化用户测评项目段位及得分
     */
    public void initUserability(Long accountId){
        Map<String, Object> items = AbilityItemEnum.getMap();
        ExaDuan duan = duanService.findMin();
        for(String key : items.keySet()){//初始化学历工作时不添加段位
            ExamType abilityItem = examTypeService.findByCode(key.toString());
            if(items.get(key).toString().equals(AbilityItemEnum.education.code) ||
                items.get(key).toString().equals(AbilityItemEnum.work.code)){
                BusUserability userability = new BusUserability();
                userability.setAbilityItemId(String.valueOf(abilityItem.getId()));
                userability.setAbilityScore("1");
                userability.setUserId(accountId);
                userability.setHrScore("0");
                save(userability);
            }else{
                BusUserability userability = new BusUserability();
                userability.setAbilityItemId(String.valueOf(abilityItem.getId()));
                userability.setAbilityScore(String.valueOf(duan.getDuanScore()));
                userability.setDuanId(String.valueOf(duan.getId()));
                userability.setUserId(accountId);
                userability.setHrScore("0");
                save(userability);
            }
        }
    }

    /**
     * 查询用户排序
     */
    public List<EntityMap> selectuser(Long companyId, Long itemId){
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(new PagerInfo());
        queryWrapper.select("b.userId");
        queryWrapper.eq("b.abilityItemId", itemId);
        if(ObjectUtils.isNotEmpty(companyId)){
            queryWrapper.eq("user.companyId", companyId);
        }
        queryWrapper.isNotNull("user.companyId");
        queryWrapper.orderByDesc("duan.sortOrder");
        queryWrapper.orderByAsc("user.createTime");
        queryWrapper.createAlias("user", "user");
        queryWrapper.createAlias("duan", "duan");
        PqGrid pqGrid = pageList(queryWrapper);
        return (List<EntityMap>)pqGrid.getRows();
    }


}
